package com.cap.dreamcircle.View;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cap.dreamcircle.App.App;
import com.cap.dreamcircle.Base.BaseActivity;
import com.cap.dreamcircle.Common.LoadMore.FootView;
import com.cap.dreamcircle.Common.LoadMore.LoadMoreUtil;
import com.cap.dreamcircle.Model.Bean.BaseBean;
import com.cap.dreamcircle.Model.Bean.SelfSimpleDreamBean;
import com.cap.dreamcircle.Model.Bean.SelfSimpleDreamRecordsBean;
import com.cap.dreamcircle.Model.IDreamModel;
import com.cap.dreamcircle.Model.Impl.DreamModelImpl;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.CommonSubscriber;
import com.cap.dreamcircle.Utils.TransformUtils;
import com.cap.dreamcircle.View.Adapter.SelfSimpleDreamAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 梦境list
 * Created by liucxu on 2017/8/10.
 */

public class DreamRecordsActivity extends BaseActivity {

    @BindView(R.id.layout_back)
    LinearLayout layout_back;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.swipe_refrsh_dream_records)
    SwipeRefreshLayout swipe_refrsh_dream_records;
    @BindView(R.id.recyclerview_dream_records)
    RecyclerView recyclerview_dream_records;

    private SelfSimpleDreamAdapter adapter;
    private List<SelfSimpleDreamBean> datas = new ArrayList<>();
    private int page_index = 1;
    private boolean is_refresh=true;

    private IDreamModel model;

    private LoadMoreUtil loadMoreUtil;

    @Override
    public int getLayoutId() {
        return R.layout.activity_dreams_records;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
        tv_title.setText("我的梦境");
        layout_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                App.GetInstance().finishActivity(DreamRecordsActivity.this);
            }
        });
        model = new DreamModelImpl();
        adapter = new SelfSimpleDreamAdapter(this, datas);
        recyclerview_dream_records.setLayoutManager(new LinearLayoutManager(this));
        recyclerview_dream_records.setAdapter(adapter);
        swipe_refrsh_dream_records.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                is_refresh=true;
                pageSimpleDreamRecords();
            }
        });
        loadMoreUtil=new LoadMoreUtil();
        loadMoreUtil.setFootView(FootView.getLayoutView(this, recyclerview_dream_records));
        loadMoreUtil.setRecyclerView(recyclerview_dream_records);
        loadMoreUtil.setOnLoadMoreListener(new LoadMoreUtil.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                is_refresh=false;
                pageSimpleDreamRecords();
            }
        });
        pageSimpleDreamRecords();
    }


    private void pageSimpleDreamRecords() {
        int local_page_index=page_index;
        if(is_refresh){
            local_page_index=1;
        }
        model.SelfSimpleDreamRecords(local_page_index).compose(TransformUtils.<SelfSimpleDreamRecordsBean>defaultSchedulers()).subscribe(new CommonSubscriber<SelfSimpleDreamRecordsBean>() {
            @Override
            public void onSuccess(SelfSimpleDreamRecordsBean response) {
                swipe_refrsh_dream_records.setRefreshing(false);
                if ((response.getDreamArray() != null) && (response.getDreamArray().size() > 0)) {
                    if(is_refresh){
                        datas.clear();
                        page_index=1;
                    }
                    datas.addAll(response.getDreamArray());
                    adapter.setDatas(datas);
                    page_index++;
                }
                else{
                    loadMoreUtil.hideFootView();
                }
            }

            @Override
            public void onFailure(BaseBean response) {
                swipe_refrsh_dream_records.setRefreshing(false);
                loadMoreUtil.hideFootView();
            }
        });
    }

    @Override
    public void setupData() {
    }
}
